package com.menghai.mongoDB;

import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;


import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.QueryOperators;
import com.mongodb.util.JSON;

public class MongoDBDao {
	/**
	 * @author menghai
	 * @since 2013-4-9
	 * 公用MongoDBDao
	 */
	
	private static MongoDBDao mongoDao = new MongoDBDao();
	private Mongo mongo = null;
	private DB db = null;
	
	private MongoDBDao(){
		//创建了一个MongoDB的数据库连接对象
		try{
			mongo = new Mongo("127.0.0.1",27017);
			//得到数据库
			db = mongo.getDB("mts");
			db.requestStart();
		}catch(UnknownHostException e){
			e.printStackTrace();
		}
	}
	public static MongoDBDao getInstance(){
		return mongoDao;
	}
	
	//得到集合
	@SuppressWarnings("unused")
	private DBCollection getMongoDBCollection(String collectionName){
		DBCollection collection = db.getCollection(collectionName);
		return collection;
	}
	public void closeRequest(){
		db.requestDone();
	}
	
	
	public static void main(String[] args) {
		MongoDBDao dao = MongoDBDao.getInstance();
		DBCollection collectionhhh = dao.getMongoDBCollection("incident");
		List<DBObject> coll = collectionhhh.find().toArray();
		
		
		
		for(DBObject o : coll){
			DBObject dbo1 = new BasicDBObject();
			dbo1.put("dimesionid",3);
			dbo1.put("posValue",5);
			dbo1.put("negValue",-1.6);
			DBObject dboject = new BasicDBObject();
			dboject.put("i_dimensions_fitment", dbo1);
			
			DBObject dbo2 = new BasicDBObject();
			dbo2.put("dimesionid",4);
			dbo2.put("posValue",5);
			dbo2.put("negValue",-1.6);
			dboject.put("i_dimensions_whole", dbo2);
			
			DBObject dbo3 = new BasicDBObject();
			dbo3.put("dimesionid",5);
			dbo3.put("posValue",5);
			dbo3.put("negValue",-1.6);
			dboject.put("i_dimensions_favorable", dbo3);
			
			DBObject dbo4 = new BasicDBObject();
			dbo4.put("dimesionid",6);
			dbo4.put("posValue",5);
			dbo4.put("negValue",-1.6);
			dboject.put("i_dimensions_location", dbo4);
			
			DBObject dbo5 = new BasicDBObject();
			dbo5.put("dimesionid",7);
			dbo5.put("posValue",5);
			dbo5.put("negValue",-1.6);
			dboject.put("i_dimensions_facility", dbo5);
			
			DBObject dbo6 = new BasicDBObject();
			dbo6.put("dimesionid",8);
			dbo6.put("posValue",5);
			dbo6.put("negValue",-1.6);
			dboject.put("i_dimensions_traffic", dbo6);
		/*	
			DBObject dbo7 = new BasicDBObject();
			dbo7.put("dimesionid",8);
			dbo7.put("posValue",5);
			dbo7.put("negValue",-1.6);
			DBObject dboject7 = new BasicDBObject();
			dboject7.put("i_dimensions_traffic", dbo7);
		*/	
			DBObject dbo8 = new BasicDBObject();
			dbo8.put("dimesionid",9);
			dbo8.put("posValue",5);
			dbo8.put("negValue",-1.6);
			dboject.put("i_dimensions_restaurant", dbo8);


			DBObject dbo9 = new BasicDBObject();
			dbo9.put("dimesionid",10);
			dbo9.put("posValue",5);
			dbo9.put("negValue",-1.6);
			dboject.put("i_dimensions_price", dbo9);
			
			DBObject dbo10 = new BasicDBObject();
			dbo10.put("dimesionid",11);
			dbo10.put("posValue",5);
			dbo10.put("negValue",-1.6);
			dboject.put("i_dimensions_environment", dbo10);
			
			
			DBObject dbo11 = new BasicDBObject();
			dbo11.put("dimesionid",12);
			dbo11.put("posValue",5);
			dbo11.put("negValue",-1.6);
			dboject.put("i_dimensions_service", dbo11);
			

			collectionhhh.update(o, new BasicDBObject("$set",new BasicDBObject("i_dimesions",dboject)));
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
//		try {
//			Mongo mon = new Mongo("192.168.1.232",27017);
//			DB db = mon.getDB("mts");
//			
//		DBCollection collection = db.getCollection("demo");
//		List<DBObject> demo = collection.find().toArray();
//		for(DBObject o :demo){
//			collectionhhh.save(o);
//			
//		}
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
		
		
		
		
		
		
		
		
		
		
		//List<DBObject> tatle = collectionhhh.find(new BasicDBObject("_id" ,new BasicDBObject(QueryOperators.NIN ,new Long[]{100l}))).toArray();
		List<DBObject> tatle = collectionhhh.find(new BasicDBObject("i_label" ,"佛山文汇公寓酒店")).toArray();
		
		//System.out.println(tatle.size());
		//DBObject ob = collectionhhh.findOne(new BasicDBObject("_id",101));
		//System.out.println(ob.get("info"));
/*		
		MM ur = new MM();
		ur.setId(9);
		ur.setName("不是");
		ur.setAge(9);
//		ur.put("id", 9);
//		ur.put("name", "环境");
//		ur.put("age", 4);
		
		collectionhhh.insert(ur);
		collectionhhh.setObjectClass(MtsUserResult.class);
		MtsUserResult mur = (MtsUserResult)collectionhhh.findOne();
		System.out.println(mur.getRdimentsionsName());
		
		
*/		
	
	}
}
